package udes.assassin;

import java.util.List;

import udes.assassin.structure.Game;
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;

public class GameListAdapter extends ArrayAdapter<Game>{
	
	private final List<Game> list;
	private final Activity context;
	
	public GameListAdapter(Activity context, List<Game> list) {
	    super(context, R.layout.game_description, list);
	    this.context = context;
	    this.list = list;
	  }

	  static class ViewHolder {
		protected TextView gameServerName;
	    protected TextView gameName;
	    protected TextView nbOfPlayerInGame;
	  }
	  
	  @Override
	  public View getView(int position, View convertView, ViewGroup parent) {
		  
	    View view = null;
	    
	    if (convertView == null) {
	      LayoutInflater inflator = context.getLayoutInflater();
	      view = inflator.inflate(R.layout.game_description, null);
	      final ViewHolder viewHolder = new ViewHolder();
	      viewHolder.gameServerName = (TextView) view.findViewById(R.id.game_server_name);
	      viewHolder.gameName = (TextView) view.findViewById(R.id.game_name);
	      viewHolder.nbOfPlayerInGame = (TextView) view.findViewById(R.id.nb_of_player_in_game);
	      view.setTag(viewHolder);
	    } 
	    else {
	      view = convertView;
	    }
	    ViewHolder holder = (ViewHolder) view.getTag();
	    holder.gameServerName.setText(list.get(position).getName());
	    holder.gameName.setText(list.get(position).getFormatedName());
	    holder.nbOfPlayerInGame.setText(list.get(position).getFormatedNbOfPlayerInGame());
	    return view;
	  }
}
